<?php

require_once('../../config.php');
require_once($CFG->libdir.'/moodlelib.php');

//for file upload, check the assigment
if (empty($_POST["a"]) and $_GET["a"]) {
	$_POST["a"] = $_GET["a"];
}
if ($_POST["a"]) {
	//do file upload
	login();
	require_once("../assignment/upload.php");
	return;
}

require_once($CFG->libdir.'/weblib.php');
require_once($CFG->libdir.'/datalib.php');
require_once($CFG->libdir.'/dmllib.php');
require_once($CFG->libdir.'/filelib.php');

/**
 * The function returns details about
 * the public methond.
 * 
 * @return array which contains details about the web service
 */
function mnet_publishes() {
    $servicelist = array();
        $service['name']             = 'etom';
        /*[Description of the get_user function]*/
            $function['name']        = 'get_user';
                // user_name argument
                $argument['type']    = 'string';
            $function['arguments'][] = $argument;
                // password argument
                $argument['type']    = 'string';
            $function['arguments'][] = $argument;
            $function['description'] = get_string($function['name'], 'etom');
        $service['functions'][]      = $function;
        /*[Description of the get_user function]*/
        
        /*[Description of the get_user_cources function]*/
        	$function['name']        = 'get_user_cources';
                // user_id argument
                $argument['type']    = 'int';
            $function['arguments'][] = $argument;
            $function['description'] = get_string($function['name'], 'etom');
        $service['functions'][]      = $function;    
        /*[Description of the get_user_cources function]*/
        
        /*[Description of the get_course_modules function]*/
        	$function['name']        = 'get_course_modules';
                // course_id argument
                $argument['type']    = 'int';
            $function['arguments'][] = $argument;
            	// module_name argument
                $argument['type']    = 'string';
                $argument['default'] = 'resource';
            $function['arguments'][] = $argument;
            $function['description'] = get_string($function['name'], 'etom');
        $service['functions'][]      = $function;    
        /*[Description of the get_course_modules function]*/
        
        /*[Description of the get_resource_url function]*/
        	$function['name']        = 'get_resource_url';
                // course_id argument
                $argument['type']    = 'int';
            $function['arguments'][] = $argument;
            	// resource_id argument
                $argument['type']    = 'int';
            $function['arguments'][] = $argument;
            $function['description'] = get_string($function['name'], 'etom');
        $service['functions'][]      = $function;    
        /*[Description of the get_resource_url function]*/
        
    $servicelist[]                   = $service;

    return $servicelist;
}

/**
 * It is a entry point for all method.
 * The method returns the identofier of the user.
 * If the user does not exist then 0 is returned.
 * 
 * @param $user_name - the user name in plain text
 * @param $password - the user's password in plain text
 * 
 * @return int - the user identifier
 */
function get_user($user_name, $password)
{
	$user = authenticate_user_login($user_name, $password);
	if ($user != false)
		complete_user_login($user);
	$user_response = new object();
	$user_response->id = $user->id;
	return $user_response;
}

/**
 * The method returns the user cources.
 * The user must be logged before the call of the method.
 * 
 * @param $user_id - the identifier of the user
 * 
 * @return array {@link $COURSE} of course objects
 */
function get_user_cources($user_id)
{
	login();
	$response = get_my_courses($user_id);
	foreach ($response as $key => $value)
	{
		$currentCourse = $response[$key];
		$currentCourseRes = new object();
		$currentCourseRes->id = $currentCourse->id;
		$currentCourseRes->fullname = $currentCourse->fullname;
		$response[$key] = $currentCourseRes;
	}
	return $response;
}

/**
 * The method returns the modules of the given course.
 * The user must be logged before the call of the method.
 * 
 * @param $course_id - the course identifier
 * @param $module_name - the module name (by default 'resource')
 * 
 * @return array of cm objects, false if not found or error
 */
function get_course_modules($course_id, $module_name='resource')
{
	login();
	$extra_field_ref = "reference";
	if (0 == strcmp('resource', $module_name)) {
		$extra_field_desc = "summary";
		//we expect summary and reference from the resource
		$extra_fields = $extra_field_desc . ", " . $extra_field_ref;
	} else {
		$extra_field_desc = "description";
		//we don't expect reference here
		$extra_fields = $extra_field_desc;
	}
	$response = get_coursemodules_in_course($module_name, $course_id, $extra_fields);
	foreach ($response as $key => $value)
	{
		$currentModule = $response[$key];
		$currentModuleRes = new object();
		$currentModuleRes->instance = $currentModule->instance;
		$currentModuleRes->name = $currentModule->name;
		$currentModuleRes->reference = $currentModule->{$extra_field_ref};
		$currentModuleRes->description = $currentModule->{$extra_field_desc};
		$response[$key] = $currentModuleRes;
	}
	return $response;
}

/**
 * The method returns the url, where the module can be downloaded.
 * The user must be logged before the call of the method.
 * 
 * @param $course_id - the identifier of the course
 * @param $resource_id - the identifier of the resource
 * 
 * @return string - url where the resource is available
 */
function get_resource_url($course_id, $resource_id)
{
	login();
	$resource_ref = get_field('resource', 'reference', 'id', $resource_id);
	return get_file_url($course_id.'/'.$resource_ref);
}

/*
 * An utility method which checks the user
 * authentication and updates the login time.
 */
function login()
{
	require_login();
	update_user_login_times();
}

?>